﻿using JNPF.Basics.Models;
using JNPF.Data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 字典数据
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20
    /// </summary>
    internal class DictionaryDataService : DataFactory, DictionaryDataIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictionaryDataEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<DictionaryDataEntity>().OrderBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="dictionaryTypeId">类别主键</param>
        /// <returns></returns>
        public async Task<List<DictionaryDataEntity>> GetList(string dictionaryTypeId)
        {
            using (var db = this.Basic)
            {

                return await db.GetIQueryable<DictionaryDataEntity>().Where(d => d.DictionaryTypeId == dictionaryTypeId).OrderBy(o => o.SortCode).ToListAsync();

            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="dictionaryTypeId">类别主键</param>
        /// <returns></returns>
        public async Task<DictionaryDataEntity> GeInfo(string enCode)
        {
            using (var db = this.Basic)
            {

                return await db.GetEntityAsync<DictionaryDataEntity>(d => d.EnCode == enCode);

            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<DictionaryDataEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<DictionaryDataEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="whereLambda">条件</param>
        /// <returns></returns>
        public async Task<bool> IsExist(Expression<Func<DictionaryDataEntity, bool>> whereLambda)
        {
            using (var db = this.Basic)
            {
                return (await db.GetCountAsync(whereLambda) > 0);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(DictionaryDataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(DictionaryDataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(DictionaryDataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

    }
}
